﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models;
using ZHDJ.Data.Models.ATM;
using ZHDJ.Data.Models.Org;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.ATM
{
    public partial class PartyDonationEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        public string SetControlEnabled
        {
            get
            {
                return Request.QueryString["SetControlEnabled"] ?? "0";
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }
        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(this.ddlTitular, SysDataDictionary.ATMTitular);
            ControlBusiness.ControlBindDataDictionary(this.ddlThingType, SysDataDictionary.ATMThingType);
            ControlBusiness.ControlBindDataDictionary(this.ddlName, SysDataDictionary.ATMServiceType);
            ControlBusiness.ControlBindDataDictionary(this.ddlSaveModus, SysDataDictionary.ATMSaveModus);
            ControlBusiness.ControlBindDataDictionary(this.ddlPurposes, SysDataDictionary.ATMPurposes);
            this.txtCreateby.Text = CurrentUser.UserName;
            this.txtCreateby.Enabled = true;
            this.txtCreatebyContact.Text = CurrentUser.MobileNo;
            this.txtCreatebyAddress.Text = CurrentUser.Address;
            PartyOrgBranch branch = DB.PartyOrgBranchs.Find(CurrentUser.BranchID);
            this.txtBranchName.Text = branch.BranchName;
            this.txtBranchContact.Text = branch.LinkPhone;
            this.txtBranchContactPerson.Text = branch.LinkMan;
            this.txtBranchAddress.Text = branch.Address;
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyATMSaveThings oModel = DB.PartyATMSaveThingss.Find(id);
                if (oModel == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                this.txtBranchName.Text = oModel.BranchName;
                this.txtBranchAddress.Text = oModel.BranchAddress;
                this.txtBranchContact.Text = oModel.BranchContact;
                this.txtBranchContactPerson.Text = oModel.BranchContactPerson;

                hdBranchID.Text = ZHDJ.Data.PartyOrg.QueryMemberBranchID(oModel.Createby);

                this.txtCreateby.Text = ZHDJ.Data.PartyOrg.GetUserMessageByloginID(oModel.Createby).Rows[0]["UserName"].ToString(); ;
                this.txtCreatebyAddress.Text = oModel.CreatebyAddress;
                this.txtCreatebyContact.Text = oModel.CreatebyContact;

                this.heDescription.Text = oModel.Description;
                this.txtPurposesDescription.Text = oModel.PurposesDescription;
                this.txtThingCount.Text = oModel.ThingCount.ToString();
                this.ddlPurposes.SelectedValue = oModel.Purposes;
                this.ddlSaveModus.SelectedValue = oModel.SaveModus;
                this.ddlStatus.SelectedValue = oModel.Status;
                this.ddlThingType.SelectedValue = oModel.ThingType;
                this.ddlTitular.SelectedValue = oModel.Titular;
                this.dtEndTime.Text = Convert.ToDateTime(oModel.EndTime).ToString("yyyy-MM-dd");

                TitularShow();
                NameEnabled();
                this.txtName.Text = oModel.Name;
                NameSelect(oModel);

                if (!oModel.Createby.Equals(this.CurrentUser.LoginID))
                {
                    ControlEnabled();
                    //ResolveFormField(delegate (Field field)
                    //{
                    //    field.Enabled = false;
                    //});
                    //ModuleUtil.SetControlEnabled(this.Controls);
                    return;
                }
                if (Convert.ToBoolean(oModel.IsSub))
                {
                    ControlEnabled();
                    //ResolveFormField(delegate (Field field)
                    //{
                    //    field.Enabled = false;
                    //});
                    ///ModuleUtil.SetControlEnabled(this.Controls);
                    return;
                }
                if (oModel.Status.Equals("1") || oModel.Status.Equals("2"))
                {
                    ControlEnabled();
                    //ResolveFormField(delegate (Field field)
                    //{
                    //    field.Enabled = false;
                    //});
                    //ModuleUtil.SetControlEnabled(this.Controls);
                    return;
                }
                if (this.SetControlEnabled.Equals("1"))
                {
                    ControlEnabled();
                    //ResolveFormField(delegate (Field field)
                    //{
                    //    field.Enabled = false;
                    //});
                    //ModuleUtil.SetControlEnabled(this.Controls);
                    return;
                }
            }
            else
            {
                this.txtName.Text = "资助金";
                this.txtName.Readonly = true;
                this.hdBranchID.Text = CurrentUser.BranchID;
            }

        }

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            var q = DB.PartyATMSaveThingss;
            PartyATMSaveThings omodel;
            if (id != "")
            {
                omodel = q.Find(id);
                if (omodel == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
                omodel.ID = id;
                omodel.CreatebyAddress = this.txtCreatebyAddress.Text;
                omodel.CreatebyContact = this.txtCreatebyContact.Text;
                omodel.BranchName = this.txtBranchName.Text;
                omodel.BranchAddress = this.txtBranchAddress.Text;
                omodel.BranchContact = this.txtBranchContact.Text;
                omodel.BranchContactPerson = this.txtBranchContactPerson.Text;
                omodel.Description = this.heDescription.Text;
                omodel.Name = this.txtName.Text;
                omodel.PurposesDescription = this.txtPurposesDescription.Text;
                omodel.ThingCount = Convert.ToInt32(this.txtThingCount.Text);
                omodel.Purposes = this.ddlPurposes.SelectedValue;
                omodel.SaveModus = this.ddlSaveModus.SelectedValue;
                omodel.Status = this.ddlStatus.SelectedValue;
                omodel.ThingType = this.ddlThingType.SelectedValue;
                omodel.Titular = this.ddlTitular.SelectedValue;
                omodel.IsEndTime = true;
                omodel.EndTime = string.IsNullOrEmpty(this.dtEndTime.Text) ? Convert.ToDateTime("2200-12-31") : Convert.ToDateTime(this.dtEndTime.Text);
                omodel.ModifyBy = this.CurrentUser.LoginID;
                omodel.ModifyTime = DateTime.Now;
                DB.SaveChanges();
            }
            else
            {
                omodel = new PartyATMSaveThings();
                omodel.ID = StringHelper.NewID;
                omodel.CreatebyAddress = this.txtCreatebyAddress.Text;
                omodel.CreatebyContact = this.txtCreatebyContact.Text;
                omodel.BranchName = this.txtBranchName.Text;
                omodel.BranchAddress = this.txtBranchAddress.Text;
                omodel.BranchContact = this.txtBranchContact.Text;
                omodel.BranchContactPerson = this.txtBranchContactPerson.Text;
                omodel.Description = this.heDescription.Text;
                omodel.Name = this.txtName.Text;
                omodel.PurposesDescription = this.txtPurposesDescription.Text;
                omodel.ThingCount = Convert.ToInt32(this.txtThingCount.Text);
                omodel.Purposes = this.ddlPurposes.SelectedValue;
                omodel.SaveModus = this.ddlSaveModus.SelectedValue;
                omodel.Status = this.ddlStatus.SelectedValue;
                omodel.ThingType = this.ddlThingType.SelectedValue;
                omodel.Titular = this.ddlTitular.SelectedValue;
                omodel.IsEndTime = true;
                omodel.EndTime = string.IsNullOrEmpty(this.dtEndTime.Text) ? Convert.ToDateTime("2200-12-31") : Convert.ToDateTime(this.dtEndTime.Text);
                omodel.ModifyBy = this.CurrentUser.LoginID;
                omodel.ModifyTime = DateTime.Now;
                omodel.Createby = this.CurrentUser.LoginID;
                omodel.CreateTime = DateTime.Now;
                omodel.BranchID = hdBranchID.Text;
                DB.PartyATMSaveThingss.Add(omodel);
                DB.SaveChanges();
            }

        }
        #region 页面展示
        public void TitularShow()
        {
            switch (this.ddlTitular.SelectedValue)
            {
                ///0 本人 1组织 2匿名
                case "0": this.pPeople1.Hidden = this.pPeople2.Hidden = false; this.pBranch1.Hidden = this.pBranch2.Hidden = true; break;
                case "1": this.pPeople1.Hidden = this.pPeople2.Hidden = true; this.pBranch1.Hidden = this.pBranch2.Hidden = false; break;
                case "2": this.pPeople1.Hidden = this.pPeople2.Hidden = true; this.pBranch1.Hidden = this.pBranch2.Hidden = true; break;
            }
        }

        public void NameEnabled()
        {
            if (this.ddlThingType.SelectedValue.Equals("0"))
            {
                this.txtName.Text = "资助金";
                this.txtName.Readonly = true;
                this.ddlName.Hidden = true;
                this.txtName.Hidden = false;
            }
            else if (this.ddlThingType.SelectedValue.Equals("2"))
            {
                this.ddlName.Hidden = false;
                this.txtName.Hidden = true;
                this.txtName.Text = this.ddlName.SelectedItem.Text;
            }
            else
            {
                this.txtName.Text = "";
                this.ddlName.Hidden = true;
                this.txtName.Hidden = false;
                this.txtName.Readonly = false;
            }
        }

        private void NameSelect(PartyATMSaveThings oModel)
        {
            if (this.ddlThingType.SelectedValue.Equals("2"))
            {
                foreach (FineUI.ListItem item in this.ddlName.Items)
                {
                    if (item.Text.Equals(oModel.Name))
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }
        }
        #endregion

        #region  按钮
        /// <summary>
        /// 捐助名义
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlTitular_SelectedIndexChanged(object sender, EventArgs e)
        {
            TitularShow();
        }

        protected void ddlPurposes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlPurposes.SelectedValue.Equals("2"))
            {
                this.txtPurposesDescription.Required = this.txtPurposesDescription.ShowRedStar = true;

            }
            else
            {
                this.txtPurposesDescription.Required = this.txtPurposesDescription.ShowRedStar = false;
            }
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            this.SaveData();
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }


        protected void ddlThingType_SelectedIndexChanged(object sender, EventArgs e)
        {
            NameEnabled();
        }

        protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtName.Text = this.ddlName.SelectedItem.Text;
        }
        #endregion

        #region 禁用所有控件
        public void ControlEnabled()
        {
            this.ddlTitular.Readonly = true;
            this.txtCreateby.Readonly = true;
            this.txtCreatebyContact.Readonly = true;
            this.txtCreatebyAddress.Readonly = true;
            this.txtBranchName.Readonly = true;
            this.txtBranchContactPerson.Readonly = true;
            this.txtBranchContact.Readonly = true;
            this.txtBranchAddress.Readonly = true;
            this.ddlThingType.Readonly = true;
            this.txtName.Readonly = true;
            this.ddlName.Readonly = true;
            this.txtThingCount.Readonly = true;
            this.dtEndTime.Readonly = true;
            this.ddlSaveModus.Readonly = true;
            this.ddlPurposes.Readonly = true;
            this.txtPurposesDescription.Readonly = true;
            this.ddlStatus.Readonly = true;
            this.heDescription.Readonly = true;
        }
        #endregion
    }
}